import { notification, message } from 'antd';
import cloneDeep from 'lodash/cloneDeep';
import _map from 'lodash/map';
import _uniq from 'lodash/uniq';
import _pull from 'lodash/pull';
import { GetDateThree, FormatnumberZero } from '../../../../function';
import {
  getOptionListCPB,
  makeOptionListCPB,
  getOptionListPreviewCPB,
  addCPB,
  getStockCpbOperateList,
  getStockProfit,
  getGeneratePosition,
  delStockOperate,
  uploadStockOperate,
} from '../../../../services/daily_service';
import { searchCode } from '../../../../services/line_service';

export default {
  namespace: 'cpb',
  state: {},

  effects: {
    *GetOptionListCPB({ payload, callback }, { call, put }) {
      const response = yield call(getOptionListCPB, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        if (callback) callback(response);
      }
    },

    *AddCPB({ payload, callback }, { call, put }) {
      const response = yield call(addCPB, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },

    *GetOptionListPreviewCPB({ payload, callback }, { call, put }) {
      const response = yield call(getOptionListPreviewCPB, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        if (callback) callback(response);
      }
    },

    *MakeOptionListCPB({ payload, callback }, { call, put }) {
      const response = yield call(makeOptionListCPB, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },

    *GetStockProfit({ payload, callback }, { call, put }) {
      const response = yield call(getStockProfit, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        if (callback) callback(response);
      }
    },

    *GetStockCpbOperateList({ payload, callback }, { call, put }) {
      const response = yield call(getStockCpbOperateList, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        if (callback) callback(response);
      }
    },

    *GetGeneratePosition({ payload, callback }, { call, put }) {
      const response = yield call(getGeneratePosition, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },
    *UploadStockOperate({ payload, callback }, { call, put }) {
      const response = yield call(uploadStockOperate, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },
    *DelStockOperate({ payload, callback }, { call, put }) {
      const response = yield call(delStockOperate, payload);
      if (callback) callback(response);
      if (typeof response === 'undefined' || response.code !== 1) {
        notification.error({
          message: typeof response.message !== 'undefined' ? response.message : '请求错误',
        });
      } else if (response.code === 1) {
        message.success(response.message);
        if (callback) callback(response);
      }
    },
  },

  reducers: {
    saveCodeList(state, action) {
      return {
        ...state,
        codeList: action.payload,
      };
    },
  },
};
